<template>
  <z-modal
      v-bind="modalOption"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
  >
    <vxe-grid
        id="GroupList"
        keep-source
        ref="vxeTable"
        :height="null"
        v-bind="options"
        @cell-click="cellClick"
    >
    </vxe-grid>
  </z-modal>
</template>

<script>
import { VxeTableMixin } from '@/mixins/VxeTableMixin'
import ModalFormMixin from '@/mixins/ModalFormMixin'
import { postAction } from '../../../services/manage'
export default {
  name: 'GroupList',
  mixins: [ModalFormMixin, VxeTableMixin],
  data() {
    return {
      columns: [
        {
          type: 'radio',
          width: '60'
        },
        {
          title: '组名',
          field: 'groupName'
        }
      ],
      url: {
        list: '/doc/group/v1/list'
      },
      orderNo: '',
      customTableToolbar: {
        buttons: [],
        export: false,
        import: false
      }
    }
  },
  methods: {
    cellClick(cell) {
      this.selectedRowKeys = [cell.row.id]
      this.$refs.vxeTable.setRadioRow(this.dataSource[cell.rowIndex])
    },
    editBefore(data) {
      this.orderNo = data.orderNo
      this.loadData()
    },
    editAfter(data) {
      this.title = '订单分配'
    },
    async handleOk() {
      const groups = this.selectedRowKeys
      if (groups.length !== 1) {
        this.$message.warn('请选择要分配的群组')
        return
      }
      const groupId = groups[0]
      this.confirmLoading = true
      postAction('/doc/order/v1/distribute', {
        orderNo: this.orderNo,
        groupId: groupId
      }).then(res => {
        this.confirmLoading = false
        if (res.success) {
          this.$message.success('订单分配成功')
          this.$emit('ok')
          this.close()
        } else {
          this.$message.error('订单分配失败：' + res.message)
        }
      })
    }
  }
}
</script>
